<?php
/**
* 前台文章控制器
*/
class ArticleController extends PlatformController
{
	
	public function indexAction()
	{
		//获取所有文章
		$cate_id=$_GET['cate_id'];
		$article=Factory::M('ArticleModel');
		$artInfo=$article->getArtInfo($cate_id);
		$this->assign('artInfo',$artInfo);
		//分页
		$rowsPrePage=9;
		$rowsCount=$article->getRowsCount($cate_id);
		$maxNum=$GLOBALS['conf']['Page']['maxNum'];
		$url="index.php?p=Home&c=Article&a=index&cate_id=$cate_id";
		$page=new Page($rowsCount,$rowsPrePage,$maxNum,$url);
		$strPage=$page->show();
		$this->assign('strPage',$strPage);
		$this->publicAction($cate_id);
		$this->display('index.html');
	}
	public function publicAction($cate_id)
	{
		//侧边子分类栏
		$category=Factory::M('CategoryModel');
		$subCate=$category->getSubCateById($cate_id);
		$this->assign('subCate',$subCate);
		//面包屑导航
		$list=$category->getAllParentCateName($cate_id);
		$this->assign('list',$list);
		//排行文章
		$article=Factory::M('ArticleModel');
		$sortByHits=$article->getSortByHits($cate_id,9);
		$this->assign('sortByHits',$sortByHits);
		//推荐文章
		$sortByRecommend=$article->getSortByRecommend($cate_id,9);
		$this->assign('sortByRecommend',$sortByRecommend);
	}
	public function showAction()
	{
		$art_id=$_GET['art_id'];
		$article=Factory::M('ArticleModel');
		$arow=$article->getArtInfoById($art_id);
		$this->assign('arow',$arow);
		$article->updateHitsById($art_id);
		$cate_id=$arow['cate_id'];
		$this->publicAction($cate_id);
		$prev=$article->getPrevArt($art_id);
		$next=$article->getNextArt($art_id);
		$this->assign('prev',$prev);
		$this->assign('next',$next);
		//评论
		$comment=Factory::M('CommentModel');
		$rowsPrePage=3;
		$rowsCount=$comment->getCmtsCount($art_id);
		$maxNum=$GLOBALS['conf']['Page']['maxNum'];
		$url="index.php?p=Home&c=Article&a=show&art_id=$art_id";
		$page=new Page($rowsCount,$rowsPrePage,$maxNum,$url);
		$strPage=$page->show();
		$this->assign('strPage',$strPage);
		$cmtInfo=$comment->getCmtInfoById($art_id);
		$this->assign('rowsCount',$rowsCount);
		$this->assign('cmtInfo',$cmtInfo);
		$this->display('show.html');
	}
	public function commentAction()
	{
		if(!isset($_SESSION['userInfo']))
		{
			$this->jump("index.php?p=Home&c=User&a=login",'请先登录');
		}
		$cmtInfo=array();
		$cmtInfo['art_id']=$_POST['art_id'];
		$cmt_content=$this->escapeData($_POST['content']);
		if(empty($cmt_content)) 
		{
			$this->jump("index.php?p=Home&c=Article&a=show&art_id=$art_id");
		}
		$cmtInfo['cmt_content']=$cmt_content;
		$cmtInfo['cmt_user']=$_SESSION['userInfo']['user_name'];
		$cmtInfo['cmt_time']=time();
		//插入数据
		$comment=Factory::M('CommentModel');
		$result=$comment->insertComment($cmtInfo);
		if($result)
		{
			$this->jump("index.php?p=Home&c=Article&a=show&art_id={$cmtInfo['art_id']}",'发表成功');
		}
		else
		{
			$this->jump("index.php?p=Home&c=Article&a=show&art_id={$cmtInfo['art_id']}",'未知错误,发表失败');
		}
	}
}